#include <iostream>
#include <algorithm>
#include <list>
#include <set>
#include <forward_list>
using namespace std;

void print(int num)
{
    cout << num << " ";
}

class Func
{
public:
    void operator()(int num)
    {
        cout << num << " ";
    }
};

int main()
{
#if 0
    //非变动性算法
    vector<int> v = {1,2,3,4,5};
    for_each(v.begin(),v.end(),print);
    Func f;
    for_each(v.begin(),v.end(),f);
    for_each(v.begin(),v.end(),[](int num){cout << num << " ";});
    cout << count(v.begin(),v.end(),3);
    cout << count_if(v.begin(),v.end(),[](int num){return num > 3;});
    cout << *max_element(v.begin(),v.end());
    cout << *find(v.begin(),v.end(),3);
    cout << distance(v.begin(), find(v.begin(),v.end(),3));
    cout << *find_if(v.begin(),v.end(),[](int num){return num > 3 && num % 2 == 0;}) <<endl;
    //寻找区间内连续的两个3，找到了返回第一个值的迭代器，没找到返回end迭代器
    cout << *search_n(v.begin(),v.end(),2,3);
    vector<int> v2 = {3,2}; 
    string s1 = "helloworld";
    string s2 = "wworld";
    cout << *search(s1.begin(),s1.end(),s2.begin(),s2.end());
    cout<<*find_end(s1.begin(),s2.end(),s2.begin(),s2.end());

    vector<int> v2 = {1,2,3};
    cout<<*find_first_of(v.begin(),v.end(),v2.begin(),v2.end());
    cout<<*adjacent_find(v.begin(),v.end());
    cout<<equal(v.begin(),v.end(),v2.end(),v2.end());

    string s1 = "hello";
    string s2 = "helwo";
    auto pair = mismatch(s1.begin(),s1.end(),s2.begin(),s2.end());
    cout << *pair.first << " " << *pair.second;
    cout << endl;
#endif
    //变动算法
    vector<int> v1 = {1,2,3};
    for_each(v1.begin(),v1.end(),[](int &num){num++;cout << num <<" ";});
    list<int> l;
    l.resize(3);

    copy(v1.begin(),v1.end(),l.begin());
    transform(v1.begin(),v1.end(),l.begin(),[](int num){return num *= 2;});

    vector<int> v3;
    v3.resize(6);
    merge(v1.begin(),v1.end(),l.begin(),l.end(),v3.begin());

    swap_ranges(v1.begin(),v1.end(),l.begin());

    for_each(l.begin(),l.end(),[](int &num){cout << num << " ";});
    copy_backward(v1.begin(),v1.end(),l.end());
    //垃圾之王，%0.1
    vector<int> v = {1,2,3};
    fill(v.begin(),v.end(),0);
    fill_n(v.begin(), 3, 100);
    generate(v.begin(),v.end(),[](){return 1;});
    generate_n(v.begin(),3,[](){return 10;});

    // replace(v.begin(), v.end(), 3, 100);
    replace_if(v.begin(),v.end(),[](int num){return num % 2 == 0;},99);
    for_each(v.begin(),v.end(),[](int num){cout << num << " ";});

    //移除性算法
    vector<int> v = {1,2,3,4,5,6};
    remove(v.begin(),v.end(),3);
    remove_if(v.begin(), v.end(),[](int num){return num > 3;});
    reverse(v.begin(),v.end());
    partition(v.begin(),v.end(),[](int num){return num  % 2 == 0;});
    for_each(v.begin(),v.end(),[](int num){cout << num << " ";});
    cout << endl;
    return 0;
}
